package com.stlm2.controller.recharge;

import com.stlm2.core.base.BaseController;
import com.stlm2.dems.entity.OrderState;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.recharge.entity.Recharge;
import com.stlm2.dems.recharge.service.RechargeService;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.codecontent.CodeContent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by zlq on 2017/8/11.
 */
@Controller
@RequestMapping("/rechargeC")
public class RechargeController extends BaseController {
    @Resource
    private RechargeService rechargeService;
    @Resource
    private UserService userService;

    /*zlq
*查查询所有用户的充值记录页面
* */
    @ResponseBody
    @RequestMapping(value = "/list", produces = "application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView) {
        modelAndView.setViewName("/recharge/payRecord");
        return modelAndView;
    }

    /*
    *zlq
    * 查询所有用户的充值记录
    * @param  token
    * @param  page:页码
    * @param  userName:用户名
    * @param  stateValue:状态值
    * @param  payChannelValue：支付渠道值
    * @param  code:订单编号
    * @param transactionNo:交易流水号
    *
     */
    @ResponseBody
    @RequestMapping(value = "/getAllRechargeRecord", produces = "application/json;charset=UTF-8")
    public String getAllRechargeRecord(ModelMap map, String page, String token, String userName, String stateValue, String payChannelValue, String code, String transactionNo) {
        String date = "";
        try {
            date = rechargeService.getUserRechargeRecord(map, page, token, userName, stateValue, payChannelValue, code, transactionNo);
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0, "充值记录获取失败", map);
        }

        return date;
    }

    /**
     *
     * @param id
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "dispose.json",produces = "application/json;charset=utf-8")
    public String dispose(Integer id, String token) {
        User user = userService.getByToken(token);
        if (user == null)
            return CodeContent.returnLogin();
        Recharge recharge = rechargeService.findEntityById(id);
        if (recharge == null)
            return CodeContent.returnFailure("没有找到充值订单！");
        if (!recharge.getOrderState().equals(OrderState.Payment))
            return CodeContent.returnFailure("不是已支付订单，无法处理！");
        recharge.setOrderState(OrderState.Finish);
        rechargeService.updateEntity(recharge);
        return CodeContent.returnSuccess(null);
    }
}
